package com.ctshk.rpc.train.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.utils.Assist;
import com.ctshk.common.utils.DateUtil;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.train.dto.*;
import com.ctshk.rpc.train.entity.*;
import com.ctshk.rpc.train.mapper.*;
import com.ctshk.rpc.train.req.*;
import com.ctshk.rpc.train.service.ITrainProductStrategyService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 火车票产品加幅策略 服务实现类
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-03-15
 */
@DubboService
public class TrainProductStrategyServiceImpl extends ServiceImpl<TrainProductStrategyMapper, TrainProductStrategy> implements ITrainProductStrategyService {

    @Autowired
    private TrainProductStrategyMapper trainProductStrategyMapper;
    @Autowired
    private TrainProductMapper trainProductMapper;

    @Autowired
    private TrainProductStrategyDateMapper trainProductStrategyDateMapper;
    @Autowired
    private TrainProductChannelOfflineAgentCustomMapper trainProductChannelOfflineAgentCustomMapper;
    @Autowired
    private TrainProductIncreaseStrategyRuleMapper trainProductIncreaseStrategyRuleMapper;
    @Autowired
    private TrainProductChannelOfflineSelfCustomMapper trainProductChannelOfflineSelfCustomMapper;
    @Autowired
    private TrainProductChannelOnlineSelfPlatformMapper trainProductChannelOnlineSelfPlatformMapper;
    @Autowired
    private TrainProductChannelOnlineThirdPlatformMapper trainProductChannelOnlineThirdPlatformMapper;
    @Autowired
    private TrainProductReservationSettingsMapper trainProductReservationSettingsMapper;
    @Autowired
    private TrainNumberMapper trainNumberMapper;
    @Autowired
    private TrainNumberSeatLevelMapper trainNumberSeatLevelMapper;


    /**
     * 产品中心-分页查询火车产品列表
     * @param req
     * @return
     */
    @Override
    public PageResponse<TrainProductManagePageDTO> queryList(TrainProductPageReq req) {
        IPage<TrainProductManagePageDTO> agentPage = new Page<>(req.getPageNo(),req.getPageSize());
        agentPage = trainProductStrategyMapper.queryList(req,agentPage);
        PageResponse<TrainProductManagePageDTO> result = new PageResponse<>(agentPage.getRecords(),agentPage.getCurrent() == 1,
                agentPage.getTotal() <= (agentPage.getCurrent() * agentPage.getSize()),
                agentPage.getTotal(), agentPage.getCurrent(), agentPage.getSize());
        return result;
    }

    /**
     * 产品中心-火车票产品加幅
     * @param trainProductId
     * @param trainProductStrategyReqList
     * @param userId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result save(Long trainProductId, List<TrainProductStrategyReq> trainProductStrategyReqList, Long userId) {
        // 校验
        Result checkResult = checkStrategyDateResult(trainProductStrategyReqList, false);
        if (null != checkResult) {
            return checkResult;
        }

        // 插入加幅策略列表
        List<TrainProductStrategy> strategyInsertList = new ArrayList<>();
        List<TrainProductStrategyDate> strategyDateInsertList = new ArrayList<>();
        List<TrainProductIncreaseStrategyRule> strategyRuleInsertList = new ArrayList<>();
        List<TrainProductChannelOfflineSelfCustom> offlineSelfCustomInsertList = new ArrayList<>();
        List<TrainProductChannelOfflineAgentCustom> offlineAgentCustomInsertList = new ArrayList<>();
        List<TrainProductChannelOnlineSelfPlatform> onlineSelfPlatformInsertList = new ArrayList<>();
        List<TrainProductChannelOnlineThirdPlatform> onlineThirdPlatformInsertList = new ArrayList<>();
        // 更新加幅策略列表
        List<TrainProductStrategy> strategyUpdateList = new ArrayList<>();
        List<TrainProductStrategyDate> strategyDateUpdateList = new ArrayList<>();
        List<TrainProductIncreaseStrategyRule> strategyRuleUpdateList = new ArrayList<>();
        List<TrainProductChannelOfflineSelfCustom> offlineSelfCustomUpdateList = new ArrayList<>();
        List<TrainProductChannelOfflineAgentCustom> offlineAgentCustomUpdateList = new ArrayList<>();
        List<TrainProductChannelOnlineSelfPlatform> onlineSelfPlatformUpdateList = new ArrayList<>();
        List<TrainProductChannelOnlineThirdPlatform> onlineThirdPlatformUpdateList = new ArrayList<>();
        // 不需删除加幅策略ID列表
        List<Long> strategyIdList = new ArrayList<>();
        List<Long> strategyDateIdList = new ArrayList<>();
        List<Long> strategyRuleIdList = new ArrayList<>();
        List<Long> offlineSelfCustomIdList = new ArrayList<>();
        List<Long> offlineAgentCustomIdList = new ArrayList<>();
        List<Long> onlineSelfPlatformIdList = new ArrayList<>();
        List<Long> onlineThirdPlatformIdList = new ArrayList<>();
        LocalDateTime nowDateTime = LocalDateTime.now();

        // 查询当前机场快线-策略IDList
        List<Long> deleteStrategyIdList = trainProductStrategyMapper.selectIdListByTrainProductIds(Arrays.asList(trainProductId));
        for (TrainProductStrategyReq strategyReq : trainProductStrategyReqList) {
            // 构建加幅策略
            TrainProductStrategy trainProductStrategy = EntityUtil.copy(strategyReq, TrainProductStrategy.class);
            if (trainProductStrategy.getId() == null) {
                trainProductStrategy.setId(SnowflakeIdWorker.nextId());
                trainProductStrategy.setCreateId(userId);
                trainProductStrategy.setGmtCreate(nowDateTime);
                strategyInsertList.add(trainProductStrategy);
            } else {
                // 剔除传过来的策略ID，剩下的是需要删除的
                if (deleteStrategyIdList.contains(trainProductStrategy.getId())) {
                    deleteStrategyIdList.remove(trainProductStrategy.getId());
                }
                trainProductStrategy.setModifiedId(userId);
                trainProductStrategy.setGmtModified(nowDateTime);
                strategyUpdateList.add(trainProductStrategy);
                strategyIdList.add(trainProductStrategy.getId());
            }
            trainProductStrategy.setTrainProductId(trainProductId);
            // 构建加幅策略日期段
            List<TrainProductStrategyDate> increaseStrategyDateList = EntityUtil.copyList(strategyReq.getTrainProductStrategyDateReqList(), TrainProductStrategyDate.class);
            for (TrainProductStrategyDate strategyDate : increaseStrategyDateList) {
                if (strategyDate.getId() == null) {
                    strategyDate.setId(SnowflakeIdWorker.nextId());
                    strategyDateInsertList.add(strategyDate);
                } else {
                    strategyDateUpdateList.add(strategyDate);
                    strategyDateIdList.add(strategyDate.getId());
                }
                strategyDate.setIncreaseStrategyId(trainProductStrategy.getId());
            }
            // 构建加幅策略-线下自营规则-所有
            if (trainProductStrategy.getSelfSalePermissionType() == Constants.SALE_CHANNEL_TYPE_ALL) {
                TrainProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(strategyReq.getTrainProductIncreaseStrategySelfRule(), TrainProductIncreaseStrategyRule.class);
                if (increaseStrategyRule.getId() == null) {
                    increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                    strategyRuleInsertList.add(increaseStrategyRule);
                } else {
                    strategyRuleUpdateList.add(increaseStrategyRule);
                    strategyRuleIdList.add(increaseStrategyRule.getId());
                }
                increaseStrategyRule.setIncreaseStrategyId(trainProductStrategy.getId());
                increaseStrategyRule.setSoldNumber(null);
                trainProductStrategy.setSelfSalePermissionId(increaseStrategyRule.getId());
            }
            // 构建加幅策略-线下自营规则-指定
            if (strategyReq.getSelfSalePermissionType() == Constants.SALE_CHANNEL_TYPE_SELECTED && CollectionUtil.isNotEmpty(strategyReq.getTrainProductChannelOfflineSelfCustomReqList())) {
                strategyReq.getTrainProductChannelOfflineSelfCustomReqList().forEach(channelOfflineSelfCustomReq -> {
                    TrainProductChannelOfflineSelfCustom channelOfflineSelfCustom = EntityUtil.copy(channelOfflineSelfCustomReq, TrainProductChannelOfflineSelfCustom.class);
                    if (channelOfflineSelfCustom.getId() == null) {
                        channelOfflineSelfCustom.setId(SnowflakeIdWorker.nextId());
                        offlineSelfCustomInsertList.add(channelOfflineSelfCustom);
                    } else {
                        offlineSelfCustomUpdateList.add(channelOfflineSelfCustom);
                        offlineSelfCustomIdList.add(channelOfflineSelfCustom.getId());
                    }
                    channelOfflineSelfCustom.setIncreaseStrategyId(trainProductStrategy.getId());
                    channelOfflineSelfCustom.setCreateId(userId);
                    channelOfflineSelfCustom.setGmtCreate(nowDateTime);
                    channelOfflineSelfCustom.setModifiedId(userId);
                    channelOfflineSelfCustom.setGmtModified(nowDateTime);
                    channelOfflineSelfCustom.setIsDeleted(IsDeletedCode.NO.getCode());
                    // 插入加幅规则
                    TrainProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOfflineSelfCustomReq.getTrainProductIncreaseStrategyRuleReq(), TrainProductIncreaseStrategyRule.class);
                    if (increaseStrategyRule.getId() == null) {
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        strategyRuleInsertList.add(increaseStrategyRule);
                    } else {
                        strategyRuleUpdateList.add(increaseStrategyRule);
                        strategyRuleIdList.add(increaseStrategyRule.getId());
                    }
                    increaseStrategyRule.setIncreaseStrategyId(trainProductStrategy.getId());
                    increaseStrategyRule.setCustomId(channelOfflineSelfCustom.getId());
                    increaseStrategyRule.setSoldNumber(null);
                });
            }
            // 构建加幅策略-线下代理规则-所有
            if (trainProductStrategy.getAgentSalePermissionType() == Constants.SALE_CHANNEL_TYPE_ALL) {
                TrainProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(strategyReq.getTrainProductIncreaseStrategyAgentRule(), TrainProductIncreaseStrategyRule.class);
                if (increaseStrategyRule.getId() == null) {
                    increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                    strategyRuleInsertList.add(increaseStrategyRule);
                } else {
                    strategyRuleUpdateList.add(increaseStrategyRule);
                    strategyRuleIdList.add(increaseStrategyRule.getId());
                }
                increaseStrategyRule.setIncreaseStrategyId(trainProductStrategy.getId());
                increaseStrategyRule.setSoldNumber(null);
                trainProductStrategy.setAgentSalePermissionId(increaseStrategyRule.getId());
            }
            // 构建加幅策略-线下代理规则-指定
            if (strategyReq.getAgentSalePermissionType() == Constants.SALE_CHANNEL_TYPE_SELECTED && com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyReq.getTrainProductChannelOfflineAgentCustomReqList())) {
                strategyReq.getTrainProductChannelOfflineAgentCustomReqList().forEach(channelOfflineAgentCustomReq -> {
                    TrainProductChannelOfflineAgentCustom channelOfflineAgentCustom = EntityUtil.copy(channelOfflineAgentCustomReq, TrainProductChannelOfflineAgentCustom.class);
                    if (channelOfflineAgentCustom.getId() == null) {
                        channelOfflineAgentCustom.setId(SnowflakeIdWorker.nextId());
                        offlineAgentCustomInsertList.add(channelOfflineAgentCustom);
                    } else {
                        offlineAgentCustomUpdateList.add(channelOfflineAgentCustom);
                        offlineAgentCustomIdList.add(channelOfflineAgentCustom.getId());
                    }
                    channelOfflineAgentCustom.setIncreaseStrategyId(trainProductStrategy.getId());
                    channelOfflineAgentCustom.setCreateId(userId);
                    channelOfflineAgentCustom.setGmtCreate(nowDateTime);
                    channelOfflineAgentCustom.setModifiedId(userId);
                    channelOfflineAgentCustom.setGmtModified(nowDateTime);
                    channelOfflineAgentCustom.setIsDeleted(IsDeletedCode.NO.getCode());
                    // 插入加幅规则
                    TrainProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOfflineAgentCustomReq.getTrainProductIncreaseStrategyRuleReq(), TrainProductIncreaseStrategyRule.class);
                    if (increaseStrategyRule.getId() == null) {
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        strategyRuleInsertList.add(increaseStrategyRule);
                    } else {
                        strategyRuleUpdateList.add(increaseStrategyRule);
                        strategyRuleIdList.add(increaseStrategyRule.getId());
                    }
                    increaseStrategyRule.setIncreaseStrategyId(trainProductStrategy.getId());
                    increaseStrategyRule.setCustomId(channelOfflineAgentCustom.getId());
                    increaseStrategyRule.setSoldNumber(null);
                });
            }
            // 线上自营
            if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyReq.getTrainProductChannelOnlineSelfPlatformReqList())) {
                strategyReq.getTrainProductChannelOnlineSelfPlatformReqList().forEach(channelOnlineSelfPlatformReq -> {
                    TrainProductChannelOnlineSelfPlatform channelOnlineSelfPlatform = EntityUtil.copy(channelOnlineSelfPlatformReq, TrainProductChannelOnlineSelfPlatform.class);
                    if (channelOnlineSelfPlatform.getId() == null) {
                        channelOnlineSelfPlatform.setId(SnowflakeIdWorker.nextId());
                        onlineSelfPlatformInsertList.add(channelOnlineSelfPlatform);
                    } else {
                        onlineSelfPlatformUpdateList.add(channelOnlineSelfPlatform);
                        onlineSelfPlatformIdList.add(channelOnlineSelfPlatform.getId());
                    }
                    channelOnlineSelfPlatform.setIncreaseStrategyId(trainProductStrategy.getId());
                    channelOnlineSelfPlatform.setCreateId(userId);
                    channelOnlineSelfPlatform.setGmtCreate(nowDateTime);
                    channelOnlineSelfPlatform.setModifiedId(userId);
                    channelOnlineSelfPlatform.setGmtModified(nowDateTime);
                    channelOnlineSelfPlatform.setIsDeleted(IsDeletedCode.NO.getCode());
                    // 插入加幅规则
                    TrainProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOnlineSelfPlatformReq.getTrainProductIncreaseStrategyRuleReq(), TrainProductIncreaseStrategyRule.class);
                    if (increaseStrategyRule.getId() == null) {
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        strategyRuleInsertList.add(increaseStrategyRule);
                    } else {
                        strategyRuleUpdateList.add(increaseStrategyRule);
                        strategyRuleIdList.add(increaseStrategyRule.getId());
                    }
                    increaseStrategyRule.setIncreaseStrategyId(trainProductStrategy.getId());
                    increaseStrategyRule.setCustomId(channelOnlineSelfPlatform.getId());
                    increaseStrategyRule.setSoldNumber(null);
                });
            }
            // 线上第三方
            if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyReq.getTrainProductChannelOnlineThirdPlatformReqList())) {
                strategyReq.getTrainProductChannelOnlineThirdPlatformReqList().forEach(channelOnlineThirdPlatformReq -> {
                    TrainProductChannelOnlineThirdPlatform channelOnlineThirdPlatform = EntityUtil.copy(channelOnlineThirdPlatformReq, TrainProductChannelOnlineThirdPlatform.class);
                    if (channelOnlineThirdPlatform.getId() == null) {
                        channelOnlineThirdPlatform.setId(SnowflakeIdWorker.nextId());
                        onlineThirdPlatformInsertList.add(channelOnlineThirdPlatform);
                    } else {
                        onlineThirdPlatformUpdateList.add(channelOnlineThirdPlatform);
                        onlineThirdPlatformIdList.add(channelOnlineThirdPlatform.getId());
                    }
                    channelOnlineThirdPlatform.setIncreaseStrategyId(trainProductStrategy.getId());
                    channelOnlineThirdPlatform.setCreateId(userId);
                    channelOnlineThirdPlatform.setGmtCreate(nowDateTime);
                    channelOnlineThirdPlatform.setModifiedId(userId);
                    channelOnlineThirdPlatform.setGmtModified(nowDateTime);
                    channelOnlineThirdPlatform.setIsDeleted(IsDeletedCode.NO.getCode());
                    // 插入加幅规则
                    TrainProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOnlineThirdPlatformReq.getTrainProductIncreaseStrategyRuleReq(), TrainProductIncreaseStrategyRule.class);
                    if (increaseStrategyRule.getId() == null) {
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        strategyRuleInsertList.add(increaseStrategyRule);
                    } else {
                        strategyRuleUpdateList.add(increaseStrategyRule);
                        strategyRuleIdList.add(increaseStrategyRule.getId());
                    }
                    increaseStrategyRule.setIncreaseStrategyId(trainProductStrategy.getId());
                    increaseStrategyRule.setCustomId(channelOnlineThirdPlatform.getId());
                    increaseStrategyRule.setSoldNumber(null);
                });
            }
        }

        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(deleteStrategyIdList)) {// 删除入参中不存在的ID列表
            trainProductStrategyMapper.delete(new QueryWrapper<TrainProductStrategy>().in("id", deleteStrategyIdList));
            trainProductStrategyDateMapper.delete(new QueryWrapper<TrainProductStrategyDate>().in("increase_strategy_id", deleteStrategyIdList));
            trainProductIncreaseStrategyRuleMapper.delete(new QueryWrapper<TrainProductIncreaseStrategyRule>().in("increase_strategy_id", deleteStrategyIdList));
            trainProductChannelOfflineSelfCustomMapper.delete(new QueryWrapper<TrainProductChannelOfflineSelfCustom>().in("increase_strategy_id", deleteStrategyIdList));
            trainProductChannelOfflineAgentCustomMapper.delete(new QueryWrapper<TrainProductChannelOfflineAgentCustom>().in("increase_strategy_id", deleteStrategyIdList));
            trainProductChannelOnlineSelfPlatformMapper.delete(new QueryWrapper<TrainProductChannelOnlineSelfPlatform>().in("increase_strategy_id", deleteStrategyIdList));
            trainProductChannelOnlineThirdPlatformMapper.delete(new QueryWrapper<TrainProductChannelOnlineThirdPlatform>().in("increase_strategy_id", deleteStrategyIdList));
        }
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyDateIdList)) {// 删除不存在的ID列表
            trainProductStrategyDateMapper.delete(new QueryWrapper<TrainProductStrategyDate>().notIn("id", strategyDateIdList));
        } else if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyIdList)) {// 删除不存在的ID列表
            trainProductStrategyDateMapper.delete(new QueryWrapper<TrainProductStrategyDate>().in("increase_strategy_id", strategyIdList));
        }
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyRuleIdList)) {
            trainProductIncreaseStrategyRuleMapper.delete(new QueryWrapper<TrainProductIncreaseStrategyRule>().notIn("id", strategyRuleIdList));
        } else if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyIdList)) {
            trainProductIncreaseStrategyRuleMapper.delete(new QueryWrapper<TrainProductIncreaseStrategyRule>().in("increase_strategy_id", strategyIdList));
        }
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(offlineSelfCustomIdList)) {
            trainProductChannelOfflineSelfCustomMapper.delete(new QueryWrapper<TrainProductChannelOfflineSelfCustom>().notIn("id", offlineSelfCustomIdList));
        } else if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyIdList)) {
            trainProductChannelOfflineSelfCustomMapper.delete(new QueryWrapper<TrainProductChannelOfflineSelfCustom>().in("increase_strategy_id", strategyIdList));
        }
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(offlineAgentCustomIdList)) {
            trainProductChannelOfflineAgentCustomMapper.delete(new QueryWrapper<TrainProductChannelOfflineAgentCustom>().notIn("id", offlineAgentCustomIdList));
        } else if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyIdList)) {
            trainProductChannelOfflineAgentCustomMapper.delete(new QueryWrapper<TrainProductChannelOfflineAgentCustom>().in("increase_strategy_id", strategyIdList));
        }
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(onlineSelfPlatformIdList)) {
            trainProductChannelOnlineSelfPlatformMapper.delete(new QueryWrapper<TrainProductChannelOnlineSelfPlatform>().notIn("id", onlineSelfPlatformIdList));
        } else if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyIdList)) {
            trainProductChannelOnlineSelfPlatformMapper.delete(new QueryWrapper<TrainProductChannelOnlineSelfPlatform>().in("increase_strategy_id", strategyIdList));
        }
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(onlineThirdPlatformIdList)) {
            trainProductChannelOnlineThirdPlatformMapper.delete(new QueryWrapper<TrainProductChannelOnlineThirdPlatform>().notIn("id", onlineThirdPlatformIdList));
        } else if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(strategyIdList)) {
            trainProductChannelOnlineThirdPlatformMapper.delete(new QueryWrapper<TrainProductChannelOnlineThirdPlatform>().in("increase_strategy_id", strategyIdList));
        }
        // 更新
        updateBatchSomeColumn(strategyUpdateList, trainProductStrategyMapper);
        updateBatchSomeColumn(strategyDateUpdateList, trainProductStrategyDateMapper);
        updateBatchSomeColumn(strategyRuleUpdateList, trainProductIncreaseStrategyRuleMapper);
        updateBatchSomeColumn(offlineSelfCustomUpdateList, trainProductChannelOfflineSelfCustomMapper);
        updateBatchSomeColumn(offlineAgentCustomUpdateList, trainProductChannelOfflineAgentCustomMapper);
        updateBatchSomeColumn(onlineSelfPlatformUpdateList, trainProductChannelOnlineSelfPlatformMapper);
        updateBatchSomeColumn(onlineThirdPlatformUpdateList, trainProductChannelOnlineThirdPlatformMapper);
        // 插入
        insertBatchSomeColumn(strategyInsertList, trainProductStrategyMapper);
        insertBatchSomeColumn(strategyDateInsertList, trainProductStrategyDateMapper);
        insertBatchSomeColumn(strategyRuleInsertList, trainProductIncreaseStrategyRuleMapper);
        insertBatchSomeColumn(offlineSelfCustomInsertList, trainProductChannelOfflineSelfCustomMapper);
        insertBatchSomeColumn(offlineAgentCustomInsertList, trainProductChannelOfflineAgentCustomMapper);
        insertBatchSomeColumn(onlineSelfPlatformInsertList, trainProductChannelOnlineSelfPlatformMapper);
        insertBatchSomeColumn(onlineThirdPlatformInsertList, trainProductChannelOnlineThirdPlatformMapper);

        // 保存成功修改火车票产品上架状态
        TrainProduct trainProduct = new TrainProduct();
        trainProduct.setId(trainProductId);
        trainProduct.setOnShelfStatus(Constants.IS_ONSHELF);
        trainProductMapper.updateById(trainProduct);
        return Result.success();
    }

    /** 火车票产品批量加幅保存 */
    @Override
    public Result saveBatch(TrainProductStrategySaveBatchReq trainProductStrategySaveBatchReq, Long userId) {
        List<Long> trainProductIdList = trainProductStrategySaveBatchReq.getTrainProductIdList();
        List<TrainProductStrategyReq> trainProductStrategyReqList = trainProductStrategySaveBatchReq.getTrainProductStrategyReqList();
        // 校验
        Result checkResult = checkStrategyDateResult(trainProductStrategyReqList, true);
        if (null != checkResult) {
            return checkResult;
        }
        LocalDateTime nowDateTime = LocalDateTime.now();
        // 删除加幅策略
        deleteStrategyByAirportExpressId(trainProductIdList);
        // 插入加幅策略
        List<TrainProductStrategy> trainProductStrategyList = new ArrayList<>();
        List<TrainProductIncreaseStrategyRule> trainProductIncreaseStrategyRuleList = new ArrayList<>();
        List<TrainProductStrategyDate> trainProductStrategyDateList = new ArrayList<>();
        List<TrainProductChannelOfflineAgentCustom> trainProductChannelOfflineAgentCustomList = new ArrayList<>();
        List<TrainProductChannelOfflineSelfCustom> trainProductChannelOfflineSelfCustomList = new ArrayList<>();
        List<TrainProductChannelOnlineSelfPlatform> trainProductChannelOnlineSelfPlatformList = new ArrayList<>();
        List<TrainProductChannelOnlineThirdPlatform> trainProductChannelOnlineThirdPlatformList = new ArrayList<>();
        for (Long trainProductId : trainProductIdList) {
            TrainProduct trainProduct = trainProductMapper.assertExistById(trainProductId);

            for (TrainProductStrategyReq increaseStrategyReq : trainProductStrategyReqList) {
                // 构建加幅策略
                TrainProductStrategy increaseStrategy = EntityUtil.copy(increaseStrategyReq, TrainProductStrategy.class);
                increaseStrategy.setTrainProductId(trainProductId);
                increaseStrategy.setId(SnowflakeIdWorker.nextId());
                // 构建加幅策略日期段
                List<TrainProductStrategyDate> increaseStrategyDateList = EntityUtil.copyList(increaseStrategyReq.getTrainProductStrategyDateReqList(), TrainProductStrategyDate.class);
                for (TrainProductStrategyDate strategyDate : increaseStrategyDateList) {
                    strategyDate.setId(SnowflakeIdWorker.nextId());
                    strategyDate.setIncreaseStrategyId(increaseStrategy.getId());
                }
                trainProductStrategyDateList.addAll(increaseStrategyDateList);
                // 构建加幅策略-线下自营规则-所有
                if (increaseStrategy.getSelfSalePermissionType() == Constants.SALE_CHANNEL_TYPE_ALL) {
                    TrainProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(increaseStrategyReq.getTrainProductIncreaseStrategySelfRule(), TrainProductIncreaseStrategyRule.class);
                    increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                    increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                    increaseStrategyRule.setSoldNumber(null);
                    increaseStrategy.setSelfSalePermissionId(increaseStrategyRule.getId());
                    trainProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                }
                // 构建加幅策略-线下自营规则-指定
                if (increaseStrategyReq.getSelfSalePermissionType() == Constants.SALE_CHANNEL_TYPE_SELECTED && CollectionUtils.isNotEmpty(increaseStrategyReq.getTrainProductChannelOfflineSelfCustomReqList())) {
                    increaseStrategyReq.getTrainProductChannelOfflineSelfCustomReqList().forEach(channelOfflineSelfCustomReq -> {
                        TrainProductChannelOfflineSelfCustom channelOfflineSelfCustom = EntityUtil.copy(channelOfflineSelfCustomReq, TrainProductChannelOfflineSelfCustom.class);
                        channelOfflineSelfCustom.setId(SnowflakeIdWorker.nextId());
                        channelOfflineSelfCustom.setIncreaseStrategyId(increaseStrategy.getId());
                        channelOfflineSelfCustom.setCreateId(userId);
                        channelOfflineSelfCustom.setGmtCreate(nowDateTime);
                        channelOfflineSelfCustom.setModifiedId(userId);
                        channelOfflineSelfCustom.setGmtModified(nowDateTime);
                        channelOfflineSelfCustom.setIsDeleted(IsDeletedCode.NO.getCode());
                        trainProductChannelOfflineSelfCustomList.add(channelOfflineSelfCustom);
                        // 插入加幅规则
                        TrainProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOfflineSelfCustomReq.getTrainProductIncreaseStrategyRuleReq(), TrainProductIncreaseStrategyRule.class);
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                        increaseStrategyRule.setCustomId(channelOfflineSelfCustom.getId());
                        increaseStrategyRule.setSoldNumber(null);
                        trainProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                    });
                }
                // 构建加幅策略-线下代理规则-所有
                if (increaseStrategy.getAgentSalePermissionType() == Constants.SALE_CHANNEL_TYPE_ALL) {
                    TrainProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(increaseStrategyReq.getTrainProductIncreaseStrategyAgentRule(), TrainProductIncreaseStrategyRule.class);
                    increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                    increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                    increaseStrategyRule.setSoldNumber(null);
                    increaseStrategy.setAgentSalePermissionId(increaseStrategyRule.getId());
                    trainProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                }
                // 构建加幅策略-线下代理规则-指定
                if (increaseStrategyReq.getAgentSalePermissionType() == Constants.SALE_CHANNEL_TYPE_SELECTED && CollectionUtils.isNotEmpty(increaseStrategyReq.getTrainProductChannelOfflineAgentCustomReqList())) {
                    for (TrainProductChannelOfflineAgentCustomReq channelOfflineAgentCustomReq : increaseStrategyReq.getTrainProductChannelOfflineAgentCustomReqList()) {
                        TrainProductChannelOfflineAgentCustom channelOfflineAgentCustom = EntityUtil.copy(channelOfflineAgentCustomReq, TrainProductChannelOfflineAgentCustom.class);
                        channelOfflineAgentCustom.setId(SnowflakeIdWorker.nextId());
                        channelOfflineAgentCustom.setIncreaseStrategyId(increaseStrategy.getId());
                        channelOfflineAgentCustom.setCreateId(userId);
                        channelOfflineAgentCustom.setGmtCreate(nowDateTime);
                        channelOfflineAgentCustom.setModifiedId(userId);
                        channelOfflineAgentCustom.setGmtModified(nowDateTime);
                        channelOfflineAgentCustom.setIsDeleted(IsDeletedCode.NO.getCode());
                        trainProductChannelOfflineAgentCustomList.add(channelOfflineAgentCustom);
                        // 插入加幅规则
                        TrainProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOfflineAgentCustomReq.getTrainProductIncreaseStrategyRuleReq(), TrainProductIncreaseStrategyRule.class);
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                        increaseStrategyRule.setCustomId(channelOfflineAgentCustom.getId());
                        increaseStrategyRule.setSoldNumber(null);
                        trainProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                    }
                }
                // 线上自营
                if (CollectionUtils.isNotEmpty(increaseStrategyReq.getTrainProductChannelOnlineSelfPlatformReqList())) {
                    increaseStrategyReq.getTrainProductChannelOnlineSelfPlatformReqList().forEach(channelOnlineSelfPlatformReq -> {
                        TrainProductChannelOnlineSelfPlatform channelOnlineSelfPlatform = EntityUtil.copy(channelOnlineSelfPlatformReq, TrainProductChannelOnlineSelfPlatform.class);
                        channelOnlineSelfPlatform.setId(SnowflakeIdWorker.nextId());
                        channelOnlineSelfPlatform.setIncreaseStrategyId(increaseStrategy.getId());
                        channelOnlineSelfPlatform.setCreateId(userId);
                        channelOnlineSelfPlatform.setGmtCreate(nowDateTime);
                        channelOnlineSelfPlatform.setModifiedId(userId);
                        channelOnlineSelfPlatform.setGmtModified(nowDateTime);
                        channelOnlineSelfPlatform.setIsDeleted(IsDeletedCode.NO.getCode());
                        trainProductChannelOnlineSelfPlatformList.add(channelOnlineSelfPlatform);
                        // 插入加幅规则
                        TrainProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOnlineSelfPlatformReq.getTrainProductIncreaseStrategyRuleReq(), TrainProductIncreaseStrategyRule.class);
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                        increaseStrategyRule.setCustomId(channelOnlineSelfPlatform.getId());
                        increaseStrategyRule.setSoldNumber(null);
                        trainProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                    });
                }
                // 线上第三方
                if (CollectionUtils.isNotEmpty(increaseStrategyReq.getTrainProductChannelOnlineThirdPlatformReqList())) {
                    increaseStrategyReq.getTrainProductChannelOnlineThirdPlatformReqList().forEach(channelOnlineThirdPlatformReq -> {
                        TrainProductChannelOnlineThirdPlatform channelOnlineSelfPlatform = EntityUtil.copy(channelOnlineThirdPlatformReq, TrainProductChannelOnlineThirdPlatform.class);
                        channelOnlineSelfPlatform.setId(SnowflakeIdWorker.nextId());
                        channelOnlineSelfPlatform.setIncreaseStrategyId(increaseStrategy.getId());
                        channelOnlineSelfPlatform.setCreateId(userId);
                        channelOnlineSelfPlatform.setGmtCreate(nowDateTime);
                        channelOnlineSelfPlatform.setModifiedId(userId);
                        channelOnlineSelfPlatform.setGmtModified(nowDateTime);
                        channelOnlineSelfPlatform.setIsDeleted(IsDeletedCode.NO.getCode());
                        trainProductChannelOnlineThirdPlatformList.add(channelOnlineSelfPlatform);
                        // 插入加幅规则
                        TrainProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOnlineThirdPlatformReq.getTrainProductIncreaseStrategyRuleReq(), TrainProductIncreaseStrategyRule.class);
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                        increaseStrategyRule.setCustomId(channelOnlineSelfPlatform.getId());
                        increaseStrategyRule.setSoldNumber(null);
                        trainProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                    });
                }
                trainProductStrategyList.add(increaseStrategy);
            }
        }
        insertBatchSomeColumn(trainProductStrategyList, trainProductStrategyMapper);
        insertBatchSomeColumn(trainProductStrategyDateList, trainProductStrategyDateMapper);
        insertBatchSomeColumn(trainProductIncreaseStrategyRuleList, trainProductIncreaseStrategyRuleMapper);
        insertBatchSomeColumn(trainProductChannelOfflineAgentCustomList, trainProductChannelOfflineAgentCustomMapper);
        insertBatchSomeColumn(trainProductChannelOfflineSelfCustomList, trainProductChannelOfflineSelfCustomMapper);
        insertBatchSomeColumn(trainProductChannelOnlineSelfPlatformList, trainProductChannelOnlineSelfPlatformMapper);
        insertBatchSomeColumn(trainProductChannelOnlineThirdPlatformList, trainProductChannelOnlineThirdPlatformMapper);
        return Result.success();
    }


    /**
     * 根据产品id查询产品加幅详情信息
     * @param productId
     * @return
     */
    @Override
    public List<TrainProductStrategyDTO> queryByProductId(Long productId) {
        // 查询加幅策略列表
        QueryWrapper<TrainProductStrategy> aeIncreaseStrategyWrapper = new QueryWrapper<>();
        aeIncreaseStrategyWrapper.eq("train_product_id", productId);
        List<TrainProductStrategy> trainProductStrategyList = trainProductStrategyMapper.selectList(aeIncreaseStrategyWrapper);
        List<TrainProductStrategyDTO> trainProductStrategyDTOList = EntityUtil.copyList(trainProductStrategyList, TrainProductStrategyDTO.class);
        for (TrainProductStrategyDTO aeIncreaseStrategyDTO : trainProductStrategyDTOList) {
            // 查询加幅策略-日期列表
            QueryWrapper<TrainProductStrategyDate> aeIncreaseStrategyDateWrapper = new QueryWrapper<>();
            aeIncreaseStrategyDateWrapper.eq("increase_strategy_id", aeIncreaseStrategyDTO.getId());
            List<TrainProductStrategyDate> aeIncreaseStrategyDates = trainProductStrategyDateMapper.selectList(aeIncreaseStrategyDateWrapper);
            aeIncreaseStrategyDTO.setTrainProductStrategyDateDTOList(EntityUtil.copyList(aeIncreaseStrategyDates, TrainProductStrategyDateDTO.class));
            // 查询加幅策略-规则列表
            QueryWrapper<TrainProductIncreaseStrategyRule> aeIncreaseStrategyRuleWrapper = new QueryWrapper<>();
            aeIncreaseStrategyRuleWrapper.eq("increase_strategy_id", aeIncreaseStrategyDTO.getId());
            List<TrainProductIncreaseStrategyRule> aeIncreaseStrategyRules = trainProductIncreaseStrategyRuleMapper.selectList(aeIncreaseStrategyRuleWrapper);
            List<TrainProductIncreaseStrategyRuleDTO> aeIncreaseStrategyRuleDTOs = EntityUtil.copyList(aeIncreaseStrategyRules, TrainProductIncreaseStrategyRuleDTO.class);
            if (Constants.SALE_CHANNEL_TYPE_ALL == aeIncreaseStrategyDTO.getSelfSalePermissionType()) {
                List<TrainProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> aeIncreaseStrategyDTO.getSelfSalePermissionId().equals(ruleDTO.getId())).collect(Collectors.toList());
                aeIncreaseStrategyDTO.setTrainIncreaseStrategySelfRule(ruleList.get(0));
                aeIncreaseStrategyRuleDTOs.remove(ruleList.get(0));
            }
            if (Constants.SALE_CHANNEL_TYPE_ALL == aeIncreaseStrategyDTO.getAgentSalePermissionType()) {
                List<TrainProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> aeIncreaseStrategyDTO.getAgentSalePermissionId().equals(ruleDTO.getId())).collect(Collectors.toList());
                aeIncreaseStrategyDTO.setTrainIncreaseStrategyAgentRule(ruleList.get(0));
                aeIncreaseStrategyRuleDTOs.remove(ruleList.get(0));
            }
            // 查询加幅策略-线下自营
            if (Constants.SALE_CHANNEL_TYPE_SELECTED == aeIncreaseStrategyDTO.getSelfSalePermissionType()) {
                QueryWrapper<TrainProductChannelOfflineSelfCustom> aeChannelOfflineSelfCustomWrapper = new QueryWrapper<>();
                aeChannelOfflineSelfCustomWrapper.eq("increase_strategy_id", aeIncreaseStrategyDTO.getId());
                aeChannelOfflineSelfCustomWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
                List<TrainProductChannelOfflineSelfCustom> aeChannelOfflineSelfCustoms = trainProductChannelOfflineSelfCustomMapper.selectList(aeChannelOfflineSelfCustomWrapper);
                List<TrainProductChannelOfflineSelfCustomDTO> aeChannelOfflineSelfCustomDTOs = EntityUtil.copyList(aeChannelOfflineSelfCustoms, TrainProductChannelOfflineSelfCustomDTO.class);
                // 组装所属加幅规则
                for (TrainProductChannelOfflineSelfCustomDTO aeChannelOfflineSelfCustomDTO : aeChannelOfflineSelfCustomDTOs) {
                    List<TrainProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> ruleDTO.getCustomId().equals(aeChannelOfflineSelfCustomDTO.getId())).collect(Collectors.toList());
                    aeChannelOfflineSelfCustomDTO.setTrainProductIncreaseStrategyRuleDTO(ruleList.get(0));
                }
                aeIncreaseStrategyDTO.setTrainProductChannelOfflineSelfCustomDTOList(aeChannelOfflineSelfCustomDTOs);
            }
            // 查询加幅策略-线下代理
            if (Constants.SALE_CHANNEL_TYPE_SELECTED == aeIncreaseStrategyDTO.getAgentSalePermissionType()) {
                QueryWrapper<TrainProductChannelOfflineAgentCustom> aeChannelOfflineAgentCustomWrapper = new QueryWrapper<>();
                aeChannelOfflineAgentCustomWrapper.eq("increase_strategy_id", aeIncreaseStrategyDTO.getId());
                aeChannelOfflineAgentCustomWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
                List<TrainProductChannelOfflineAgentCustom> aeChannelOfflineAgentCustoms = trainProductChannelOfflineAgentCustomMapper.selectList(aeChannelOfflineAgentCustomWrapper);
                List<TrainProductChannelOfflineAgentCustomDTO> aeChannelOfflineAgentCustomDTOs = EntityUtil.copyList(aeChannelOfflineAgentCustoms, TrainProductChannelOfflineAgentCustomDTO.class);
                // 组装所属加幅规则
                for (TrainProductChannelOfflineAgentCustomDTO aeChannelOfflineAgentCustomDTO : aeChannelOfflineAgentCustomDTOs) {
                    List<TrainProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> ruleDTO.getCustomId().equals(aeChannelOfflineAgentCustomDTO.getId())).collect(Collectors.toList());
                    aeChannelOfflineAgentCustomDTO.setTrainProductIncreaseStrategyRuleDTO(ruleList.get(0));
                }
                aeIncreaseStrategyDTO.setTrainProductChannelOfflineAgentCustomDTOList(aeChannelOfflineAgentCustomDTOs);
            }
            // 查询加幅策略-线上自营
            QueryWrapper<TrainProductChannelOnlineSelfPlatform> aeIncreaseChannelOnlineSelfPlatformWrapper = new QueryWrapper<>();
            aeIncreaseChannelOnlineSelfPlatformWrapper.eq("increase_strategy_id", aeIncreaseStrategyDTO.getId());
            aeIncreaseChannelOnlineSelfPlatformWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
            List<TrainProductChannelOnlineSelfPlatform> aeChannelOnlineSelfPlatforms = trainProductChannelOnlineSelfPlatformMapper.selectList(aeIncreaseChannelOnlineSelfPlatformWrapper);
            List<TrainProductChannelOnlineSelfPlatformDTO> aeChannelOnlineSelfPlatformDTOs = EntityUtil.copyList(aeChannelOnlineSelfPlatforms, TrainProductChannelOnlineSelfPlatformDTO.class);
            // 组装所属加幅规则
            for (TrainProductChannelOnlineSelfPlatformDTO aeChannelOnlineSelfPlatformDTO : aeChannelOnlineSelfPlatformDTOs) {
                List<TrainProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> ruleDTO.getCustomId().equals(aeChannelOnlineSelfPlatformDTO.getId())).collect(Collectors.toList());
                if(CollectionUtil.isNotEmpty(ruleList)) {
                    aeChannelOnlineSelfPlatformDTO.setTrainProductIncreaseStrategyRuleDTO(ruleList.get(0));
                }
            }
            aeIncreaseStrategyDTO.setTrainProductChannelOnlineSelfPlatformDTOList(aeChannelOnlineSelfPlatformDTOs);
            // 查询加幅策略-线上第三方
            QueryWrapper<TrainProductChannelOnlineThirdPlatform> aeChannelOnlineThirdWrapper = new QueryWrapper<>();
            aeChannelOnlineThirdWrapper.eq("increase_strategy_id", aeIncreaseStrategyDTO.getId());
            aeChannelOnlineThirdWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
            List<TrainProductChannelOnlineThirdPlatform> aeChannelOnlineThirds = trainProductChannelOnlineThirdPlatformMapper.selectList(aeChannelOnlineThirdWrapper);
            List<TrainProductChannelOnlineThirdPlatformDTO> aeChannelOnlineThirdPlatformDTOs = EntityUtil.copyList(aeChannelOnlineThirds, TrainProductChannelOnlineThirdPlatformDTO.class);
            // 组装所属加幅规则
            for (TrainProductChannelOnlineThirdPlatformDTO aeChannelOnlineThirdPlatformDTO : aeChannelOnlineThirdPlatformDTOs) {
                List<TrainProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> ruleDTO.getCustomId().equals(aeChannelOnlineThirdPlatformDTO.getId())).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(ruleList)) {
                    aeChannelOnlineThirdPlatformDTO.setTrainProductIncreaseStrategyRuleDTO(ruleList.get(0));
                }
            }
            aeIncreaseStrategyDTO.setTrainProductChannelOnlineThirdPlatformDTOList(aeChannelOnlineThirdPlatformDTOs);
        }
        return trainProductStrategyDTOList;
    }


    /**
     * 产品中心-查询详情
     * @param productId
     * @return
     */
    @Override
    public Result<TrainProductDetailDTO> selectDetail(Long productId) {
        TrainProduct trainProduct = trainProductMapper.selectById(productId);
        TrainProductDetailDTO trainProductDetailDTO = EntityUtil.copy(trainProduct, TrainProductDetailDTO.class);

        TrainProductReservationSettings trainProductReservationSettings = trainProductReservationSettingsMapper.selectOne(new QueryWrapper<TrainProductReservationSettings>().lambda().eq(TrainProductReservationSettings::getTrainProductId,productId));
        if (!Objects.isNull(trainProductReservationSettings)) {
            BeanUtils.copyProperties(trainProductReservationSettings,trainProductDetailDTO);
        }
        if (trainProduct.getTrainNumberId() != null) {
            TrainNumber trainNumber = trainNumberMapper.selectById(trainProduct.getTrainNumberId());
            if(!Objects.isNull(trainNumber)){
                trainProductDetailDTO.setTypeName(trainNumber.getTypeName());
                trainProductDetailDTO.setTrainNumberName(trainNumber.getTrainNumberName());
            }

            List<TrainNumberSeatLevel> trainNumberSeatLevelList = trainNumberSeatLevelMapper.selectList(new QueryWrapper<TrainNumberSeatLevel>().lambda().eq(TrainNumberSeatLevel::getTrainNumberId,trainNumber.getId()));
            String seatLevelName = trainNumberSeatLevelList.stream().map(n -> n.getSeatLevelName().toString()).collect(Collectors.joining(","));
            trainProductDetailDTO.setSeatLevelName(seatLevelName);
        }

        // 查询加幅策略列表
        QueryWrapper<TrainProductStrategy> strategyQueryWrapper = new QueryWrapper<>();
        strategyQueryWrapper.eq("train_product_id", productId);
        List<TrainProductStrategy> trainProductStrategyList = trainProductStrategyMapper.selectList(strategyQueryWrapper);
        TreeMap<LocalDate, List<TrainProductIncreaseStrategyRuleDTO>> dateRuleMap = buildAirportExpressList(trainProductStrategyList);
        trainProductDetailDTO.setDateRuleInfoMap(dateRuleMap);
        return Result.success(trainProductDetailDTO);
    }

    /**
     * 组装每天的加幅策略表
     *
     * @param strategyList
     * @return
     */
    private TreeMap<LocalDate, List<TrainProductIncreaseStrategyRuleDTO>> buildAirportExpressList(List<TrainProductStrategy> strategyList) {
        HashMap<LocalDate, List<TrainProductIncreaseStrategyRuleDTO>> localDateRuleInfoMap = new HashMap<>();
        for (TrainProductStrategy trainProductStrategy : strategyList) {
            List<TrainProductIncreaseStrategyRuleDTO> strategyRuleInfoDTOList = new ArrayList<>();
            // 查询当前加幅策略-所有规则列表
            QueryWrapper<TrainProductIncreaseStrategyRule> strategyRuleQueryWrapper = new QueryWrapper<>();
            strategyRuleQueryWrapper.eq("increase_strategy_id", trainProductStrategy.getId());
            List<TrainProductIncreaseStrategyRule> strategyRuleList = trainProductIncreaseStrategyRuleMapper.selectList(strategyRuleQueryWrapper);
            // 加幅策略-所有线下自营
            if (Constants.SALE_CHANNEL_TYPE_ALL == trainProductStrategy.getSelfSalePermissionType()) {
                List<TrainProductIncreaseStrategyRule> ruleList = strategyRuleList.stream().filter(rule -> trainProductStrategy.getSelfSalePermissionId().equals(rule.getId())).collect(Collectors.toList());
                TrainProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), TrainProductIncreaseStrategyRuleDTO.class);
                strategyRuleInfoDTO.setChannelFullName("线下自营渠道-所有");
                if (strategyRuleInfoDTO.getSaleableType() == Constants.SALE_TYPE_LIMIT) {
                    int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                    strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                }
                strategyRuleInfoDTOList.add(strategyRuleInfoDTO);
            }
            // 加幅策略-所有线下代理
            if (Constants.SALE_CHANNEL_TYPE_ALL == trainProductStrategy.getAgentSalePermissionType()) {
                List<TrainProductIncreaseStrategyRule> ruleList = strategyRuleList.stream().filter(rule -> trainProductStrategy.getAgentSalePermissionId().equals(rule.getId())).collect(Collectors.toList());
                TrainProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), TrainProductIncreaseStrategyRuleDTO.class);
                strategyRuleInfoDTO.setChannelFullName("线下代理渠道-所有");
                if (strategyRuleInfoDTO.getSaleableType() == Constants.SALE_TYPE_LIMIT) {
                    int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                    strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                }
                strategyRuleInfoDTOList.add(strategyRuleInfoDTO);
            }
            // 查询加幅策略-选择线下自营
            if (Constants.SALE_CHANNEL_TYPE_SELECTED == trainProductStrategy.getSelfSalePermissionType()) {
                QueryWrapper<TrainProductChannelOfflineSelfCustom> selfCustomQueryWrapper = new QueryWrapper<>();
                selfCustomQueryWrapper.eq("increase_strategy_id", trainProductStrategy.getId());
                selfCustomQueryWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
                List<TrainProductChannelOfflineSelfCustom> offlineSelfCustomList = trainProductChannelOfflineSelfCustomMapper.selectList(selfCustomQueryWrapper);
                // 组装所属加幅规则
                for (TrainProductChannelOfflineSelfCustom offlineSelfCustom : offlineSelfCustomList) {
                    List<TrainProductIncreaseStrategyRule> ruleList = strategyRuleList.stream().filter(rule -> rule.getCustomId().equals(offlineSelfCustom.getId())).collect(Collectors.toList());
                    TrainProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), TrainProductIncreaseStrategyRuleDTO.class);
                    strategyRuleInfoDTO.setChannelFullName("线下自营渠道-" + offlineSelfCustom.getName());
                    if (strategyRuleInfoDTO.getSaleableType() == Constants.SALE_TYPE_LIMIT) {
                        int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                        strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                    }
                    strategyRuleInfoDTOList.add(strategyRuleInfoDTO);
                }
            }
            // 查询加幅策略-选择线下代理
            if (Constants.SALE_CHANNEL_TYPE_SELECTED == trainProductStrategy.getAgentSalePermissionType()) {
                QueryWrapper<TrainProductChannelOfflineAgentCustom> agentCustomQueryWrapper = new QueryWrapper<>();
                agentCustomQueryWrapper.eq("increase_strategy_id", trainProductStrategy.getId());
                agentCustomQueryWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
                List<TrainProductChannelOfflineAgentCustom> offlineAgentCustomList = trainProductChannelOfflineAgentCustomMapper.selectList(agentCustomQueryWrapper);
                // 组装所属加幅规则
                for (TrainProductChannelOfflineAgentCustom offlineAgentCustom : offlineAgentCustomList) {
                    List<TrainProductIncreaseStrategyRule> ruleList = strategyRuleList.stream().filter(rule -> rule.getCustomId().equals(offlineAgentCustom.getId())).collect(Collectors.toList());
                    TrainProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), TrainProductIncreaseStrategyRuleDTO.class);
                    strategyRuleInfoDTO.setChannelFullName("线下代理渠道-" + offlineAgentCustom.getAgentName());
                    if (strategyRuleInfoDTO.getSaleableType() == Constants.SALE_TYPE_LIMIT) {
                        int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                        strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                    }
                    strategyRuleInfoDTOList.add(strategyRuleInfoDTO);
                }
            }
            // 查询加幅策略-线上自营
            QueryWrapper<TrainProductChannelOnlineSelfPlatform> onlineSelfPlatformQueryWrapper = new QueryWrapper<>();
            onlineSelfPlatformQueryWrapper.eq("increase_strategy_id", trainProductStrategy.getId());
            onlineSelfPlatformQueryWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
            List<TrainProductChannelOnlineSelfPlatform> onlineSelfPlatformList = trainProductChannelOnlineSelfPlatformMapper.selectList(onlineSelfPlatformQueryWrapper);
            // 查询加幅策略-线上自营-组装所属加幅规则
            for (TrainProductChannelOnlineSelfPlatform onlineSelfPlatform : onlineSelfPlatformList) {
                //List<TrainProductIncreaseStrategyRule> ruleList = strategyRuleList.stream().filter(rule -> rule.getCustomId().equals(onlineSelfPlatform.getId())).collect(Collectors.toList());
                List<TrainProductIncreaseStrategyRule> ruleList = Assist.filterToList(strategyRuleList, rule -> Assist.equals(rule.getCustomId(), onlineSelfPlatform.getId()));
                //if (CollectionUtil.isNotEmpty(ruleList)) {
                    TrainProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), TrainProductIncreaseStrategyRuleDTO.class);
                    strategyRuleInfoDTO.setChannelFullName("线上自营渠道-" + onlineSelfPlatform.getChannelName());
                    if (strategyRuleInfoDTO.getSaleableType() == Constants.SALE_TYPE_LIMIT) {
                        int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                        strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                    }
                    strategyRuleInfoDTOList.add(strategyRuleInfoDTO);
               // }
            }
            // 查询加幅策略-线上第三方
            QueryWrapper<TrainProductChannelOnlineThirdPlatform> onlineThirdPlatformQueryWrapper = new QueryWrapper<>();
            onlineThirdPlatformQueryWrapper.eq("increase_strategy_id", trainProductStrategy.getId());
            onlineThirdPlatformQueryWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
            List<TrainProductChannelOnlineThirdPlatform> onlineThirdPlatformList = trainProductChannelOnlineThirdPlatformMapper.selectList(onlineThirdPlatformQueryWrapper);
            // 查询加幅策略-线上第三方-组装所属加幅规则
            for (TrainProductChannelOnlineThirdPlatform onlineThirdPlatform : onlineThirdPlatformList) {
                //List<TrainProductIncreaseStrategyRule> ruleList = strategyRuleList.stream().filter(rule -> rule.getCustomId().equals(onlineThirdPlatform.getId())).collect(Collectors.toList());
                List<TrainProductIncreaseStrategyRule> ruleList = Assist.filterToList(strategyRuleList, rule -> Assist.equals(rule.getCustomId(), onlineThirdPlatform.getId()));
                if (CollectionUtil.isNotEmpty(ruleList)) {
                    TrainProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), TrainProductIncreaseStrategyRuleDTO.class);
                    strategyRuleInfoDTO.setChannelFullName("线上第三方渠道-" + onlineThirdPlatform.getPlatformName());
                    if (strategyRuleInfoDTO.getSaleableType() == Constants.SALE_TYPE_LIMIT) {
                        int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                        strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                    }
                    strategyRuleInfoDTOList.add(strategyRuleInfoDTO);
                }
            }
            // 查询加幅策略-日期列表
            QueryWrapper<TrainProductStrategyDate> strategyDateWrapper = new QueryWrapper<>();
            strategyDateWrapper.eq("increase_strategy_id", trainProductStrategy.getId());
            List<TrainProductStrategyDate> strategyDateList = trainProductStrategyDateMapper.selectList(strategyDateWrapper);
            // 组装日期map
            for (TrainProductStrategyDate strategyDate : strategyDateList) {

                long diffDay = strategyDate.getDateEnd().toEpochDay() - strategyDate.getDateBegin().toEpochDay();
                for (int i = 0; i <= diffDay; i++) {
                    localDateRuleInfoMap.put(strategyDate.getDateBegin().plusDays(i), strategyRuleInfoDTOList);
                }
            }
        }
        TreeMap<LocalDate, List<TrainProductIncreaseStrategyRuleDTO>> localDateListTreeMap = new TreeMap<>(LocalDate::compareTo);
        localDateListTreeMap.putAll(localDateRuleInfoMap);
        return localDateListTreeMap;
    }


    /**
     * 根据火车产品id删除策略
     *
     * @param trainProductIds 火车产品id列表
     */
    private void deleteStrategyByAirportExpressId(List<Long> trainProductIds) {
        if (CollectionUtils.isNotEmpty(trainProductIds)) {
            List<Long> increaseStrategyIds = trainProductStrategyMapper.selectIdListByTrainProductIds(trainProductIds);
            if (CollectionUtils.isNotEmpty(increaseStrategyIds)) {
                trainProductChannelOfflineSelfCustomMapper.delete(new QueryWrapper<TrainProductChannelOfflineSelfCustom>().lambda().in(TrainProductChannelOfflineSelfCustom::getIncreaseStrategyId, increaseStrategyIds));
                trainProductChannelOfflineAgentCustomMapper.delete(new QueryWrapper<TrainProductChannelOfflineAgentCustom>().lambda().in(TrainProductChannelOfflineAgentCustom::getIncreaseStrategyId, increaseStrategyIds));
                trainProductChannelOnlineSelfPlatformMapper.delete(new QueryWrapper<TrainProductChannelOnlineSelfPlatform>().lambda().in(TrainProductChannelOnlineSelfPlatform::getIncreaseStrategyId, increaseStrategyIds));
                trainProductChannelOnlineThirdPlatformMapper.delete(new QueryWrapper<TrainProductChannelOnlineThirdPlatform>().lambda().in(TrainProductChannelOnlineThirdPlatform::getIncreaseStrategyId, increaseStrategyIds));
                trainProductIncreaseStrategyRuleMapper.delete(new QueryWrapper<TrainProductIncreaseStrategyRule>().lambda().in(TrainProductIncreaseStrategyRule::getIncreaseStrategyId, increaseStrategyIds));
                trainProductStrategyDateMapper.delete(new QueryWrapper<TrainProductStrategyDate>().lambda().in(TrainProductStrategyDate::getIncreaseStrategyId, increaseStrategyIds));
                trainProductStrategyMapper.delete(new QueryWrapper<TrainProductStrategy>().lambda().in(TrainProductStrategy::getId, increaseStrategyIds));
            }
        }
    }



    /** 修改方法 */
    private void updateBatchSomeColumn(List list, BaseMapper mapper) {
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(list)) {
            for (Object o : list) {
                mapper.updateById(o);
            }
        }
    }

    /** 新增方法 */
    private void insertBatchSomeColumn(List list, BaseMapper mapper) {
        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(list)) {
            for (Object o : list) {
                mapper.insert(o);
            }
        }
    }




    /**
     * 策略信息校验
     * @param trainProductStrategyReqList
     * @return
     */
    private Result<Object> checkStrategyDateResult(List<TrainProductStrategyReq> trainProductStrategyReqList,boolean isbatch) {
        // 四种渠道至少有一个在售校验
        boolean saleFlag = false;
        for (TrainProductStrategyReq increaseStrategyReq : trainProductStrategyReqList) {
            // 线下-自营渠道
            if (Arrays.asList(1, 2, 3).contains(increaseStrategyReq.getSelfSalePermissionType())) {
                saleFlag = true;
                continue;
            }
            if (Arrays.asList(1, 2, 3).contains(increaseStrategyReq.getAgentSalePermissionType())) {
                saleFlag = true;
                continue;
            }
            //线下-自营渠道
            if (CollectionUtils.isNotEmpty(increaseStrategyReq.getTrainProductChannelOfflineAgentCustomReqList())) {
                saleFlag = true;
                continue;
            }
            //线下-第三方渠道
            if (CollectionUtils.isNotEmpty(increaseStrategyReq.getTrainProductChannelOfflineSelfCustomReqList())) {
                saleFlag = true;
                continue;
            }
            //线上-自营渠道
            if (CollectionUtils.isNotEmpty(increaseStrategyReq.getTrainProductChannelOnlineSelfPlatformReqList())) {
                saleFlag = true;
                continue;
            }
            //线上-第三方渠道
            if (CollectionUtils.isNotEmpty(increaseStrategyReq.getTrainProductChannelOnlineThirdPlatformReqList())) {
                saleFlag = true;
                continue;
            }
            if (!saleFlag) {
                return Result.failed(SystemError.AIREXPRESS_50003);
            }
            List<TrainProductStrategyDateReq> airportExpressIncreaseStrategyDateReqList = increaseStrategyReq.getTrainProductStrategyDateReqList();
            for (int index = 0; index < airportExpressIncreaseStrategyDateReqList.size(); index++) {
                TrainProductStrategyDateReq strategyDateReq = airportExpressIncreaseStrategyDateReqList.get(index);
                if (isbatch && strategyDateReq.getDateBegin().isBefore(LocalDate.now())) {
                    // 批量机场快线保存，开始日期不能小于当前日期校验
                    return Result.failed(SystemError.AIREXPRESS_50002);
                }
                if (index == 0 && strategyDateReq.getDateBegin().isAfter(strategyDateReq.getDateEnd())) {
                    // 开始日期必须小于结束日期校验
                    return Result.failed(SystemError.AIREXPRESS_50000);
                }
                for (int j = 0; j < airportExpressIncreaseStrategyDateReqList.size(); j++) {
                    TrainProductStrategyDateReq strategyDateReqTemp = airportExpressIncreaseStrategyDateReqList.get(j);
                    if (index >= j) { // 自身和已经校验过的跳过日期校验
                        continue;
                    }
                    if (index == 0 && strategyDateReqTemp.getDateBegin().isAfter(strategyDateReqTemp.getDateEnd())) {
                        // 开始日期必须小于结束日期校验
                        return Result.failed(SystemError.AIREXPRESS_50000);
                    }
                    if (DateUtil.isDateRange(strategyDateReq.getDateBegin(), strategyDateReq.getDateEnd(), strategyDateReqTemp.getDateBegin(), strategyDateReqTemp.getDateEnd())) {
                        // 当前日期与其他日期区域重叠校验
                        return Result.failed(SystemError.AIREXPRESS_50001);
                    }
                }
            }
        }
        return null;
    }
}
